var app = getApp(), api = getApp().api;

Page({
    data: {
        index: 0,
        items: ['红包视频', '普通视频'],
        hb_cash: '',
        hb_time: '',
        upload_video: [],
        upload_video_length: 0,
        upload_video_thumb: '',
        upload_image: [],
        upload_image_length: 0
    },

    onLoad: function (e) {
        getApp().page.onLoad(this, e);
        var t = this;
        getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        }), getApp().request({
            url: getApp().api.mch.hb_settig,
            success: function(e) {
                0 == e.code ? t.setData({
                    hb_cash: e.data.hb_cash,
                    hb_time: e.data.hb_time
                }) : getApp().core.showModal({
                    title: "提示",
                    content: e.msg,
                    showCancel: !1,
                    success: function(e) {
                        e.confirm && getApp().core.navigateBack();
                    }
                });
            },
            complete: function() {
                getApp().core.hideLoading();
            }
        });
    },

    delete: function(e) {
        this.setData({
            upload_video: [],
            upload_video_length: 0,
            upload_video_thumb: ''
        })
    },
    deleteimg: function(e) {
        var t = this;
        console.log(e);
        t.setData({
            upload_image: [],
            upload_image_length: 0
        })
    },
    chooseVideo: function() {
        var a = this, o = this.data.upload_video;
        var e = 1 - o.length;
        0 < e && e <= 1 ? getApp().core.chooseVideo({
            sourceType: ['album','camera'],
            camera: 'back',
            success: function(res) {
                getApp().core.showLoading({
                    title: "正在上传视频",
                    mask: !0
                });
                
                var t = res.tempFilePath, i = res.thumbTempFilePath;
                console.log(i);
                o = o.concat(t), a.setData({
                    upload_video: o,
                    upload_video_length: o.length,
                }), getApp().core.uploadFile({
                    url: getApp().api.default.upload_video,
                    name: "video",
                    filePath: o[0],
                    success: function(t) {
                        getApp().core.hideLoading();
                        var v = JSON.parse(t.data);
                        console.log(v);
                        if( 0 == v.code) {
                            a.setData({
                                upload_video: v.data.url,
                                upload_video_thumb: "/images/ad.png"
                            }), wx.showToast({
                              title: '视频上传成功',
                            })
                        } else {
                            wx.showModal({
                              title: '上传失败提示',
                              content: v.msg,
                              showCancel: !1,
                              success: function(res) {
                                a.setData({
                                    upload_video: [],
                                    upload_video_length: 0
                                })
                              }
                            })
                        }
                    
                    }
                });
            }
        }) : getApp().core.showModal({
            title: '上传提示',
            content: '最多上传1个视频',
            showCancel: !0,
        });
    },
    chooseImage: function() {
        var a = this, o = this.data.upload_image;
        var e = 1 - o.length;
        0  < e && e <= 1 ? getApp().core.chooseImage({
            count: 1,
            sizeType: [ "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(e) {
                getApp().core.showLoading({
                    title: "正在上传图片",
                    mask: !0
                });
                var t = e.tempFilePaths;
                o = o.concat(t), console.log(o), a.setData({
                    upload_image: o,
                    upload_image_length: o.length
                }),  getApp().core.uploadFile({
                    url: getApp().api.default.upload_image,
                    name: "image",
                    filePath: o[0],
                    complete: function(t) {
                        getApp().core.hideLoading();
                        if(t.data) {
                            var e = JSON.parse(t.data);
                            0 == e.code && a.setData({
                                upload_image: e.data.url
                            }), 1 == t.code && getApp().core.showToast({
                                title: t.msg,
                                image: "/images/icon-warning.png"
                            });
                        }
                    }
                })
            }
        }) : getApp().core.showModal({
            title: '上传提示',
            content: '最多上传1张图片',
            showCancel: !0,
        });
    },
    formSubmit: function(e) {
        var a = this, ind = a.data.index, hbcount = e.detail.value.hbcount || 0, hbmoney = e.detail.value.hbmoney || 0;
        
        if(0 == hbcount && 0 == hbmoney) {
            getApp().core.showModal({
                title: '提示',
                content: '请填写红包数量和单次金额',
                showCancel: false,
                success: function() {}
            });
        } else {
            var hbtotal = hbcount * hbmoney;
            var fee = hbtotal * a.data.hb_cash / 100;
            var money = hbtotal + fee;
            getApp().core.showModal({
                title: '提示',
                content: '此次红包金额'+hbtotal+'元加上手续费用'+fee.toFixed(2)+'元，总费用：'+money.toFixed(2)+'元。是否继续提交。',
                success: function(res) {
                    if(res.confirm) {
                        getApp().core.showLoading({
                            title: "正在提交",
                            mask: !0
                        }), getApp().request({
                            url: getApp().api.mch.video.send,
                            method: "post",
                            data: {
                                title: e.detail.value.title,
                                type1: ind,
                                looktime: e.detail.value.hbtime || 0,
                                number: e.detail.value.hbcount || 0,
                                money: e.detail.value.hbmoney || 0,
                                content: e.detail.value.textarea,
                                pic_url: a.data.upload_image,
                                url: a.data.upload_video
                            },
                            success: function(t) {
                                console.log(t);
                                1 == t.code ? getApp().core.showModal({
                                  title: '提示',
                                  content: t.msg,
                                  showCancel: !1
                                }) : getApp().core.showModal({
                                    title: "提示",
                                    content: t.msg,
                                    showCancel: !1,
                                    success: function(e) {
                                        e.confirm && 0 == t.code && getApp().core.redirectTo({
                                            url: "/mch/m/video/video-list?status=" + ind
                                        });
                                    }
                                });
                            },
                            complete: function(e) {
                                getApp().core.hideLoading();
                            }
                        })
                    }
                }
            })
        }
        
        
    },

    onReady: function() {
        getApp().page.onReady(this);
    },
    onShow: function() {
        getApp().page.onShow(this);
    },
    onHide: function() {
        getApp().page.onHide(this);
    },
    onUnload: function() {
        getApp().page.onUnload(this);
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    bindPickerChanges: function(e) {
        var t = this, o = e.detail.value;
        t.setData({
            index: o
        })
    }
})